package com.xiang.utils;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.provider.MediaStore;

public class GetMp3Data {
	Context context;
	
	public GetMp3Data(Context ctx) {
		this.context = ctx;
	}

	private Cursor getCursor(String filePath) {
		String path = null;

		Cursor c = context.getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
				MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		// System.out.println(c.getString(c.getColumnIndex("_data")));
		if (c.moveToFirst()) {
			do {
				// 通过Cursor 获取路径，如果路径相同则break；
//				System.out.println("////////" + filePath);
				path = c.getString(c
						.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
//				System.out.println("?????????" + path);
				// 查找到相同的路径则返回，此时cursorPosition 便是指向路径所指向的Cursor 便可以返回了
				if (path.equals(filePath)) {
					// System.out.println("audioPath = " + path);
					// System.out.println("filePath = " + filePath);
					// cursorPosition = c.getPosition();
					break;
				}
			} while (c.moveToNext());
		}
		// 这两个没有什么作用，调试的时候用
		// String audioPath = c.getString(c
		// .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
		//
		// System.out.println("audioPath = " + audioPath);
		return c;
	}
//
//	private String getAlbumArt(int album_id) {
//		String mUriAlbums = "content://media/external/audio/albums";
//		String[] projection = new String[] { "album_art" };
//		Cursor cur = context.getContentResolver().query(
//				Uri.parse(mUriAlbums + "/" + Integer.toString(album_id)),
//				projection, null, null, null);
//		String album_art = null;
//		if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
//			cur.moveToNext();
//			album_art = cur.getString(0);
//		}
//		cur.close();
//		cur = null;
//		return album_art;
//	}
//
//	public  BitmapDrawable getImage(String musicPath) {
//		BitmapDrawable bmpDraw = null;
//		Cursor currentCursor = getCursor(musicPath);
//		int album_id = currentCursor.getInt(currentCursor
//				.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
//		String albumArt = getAlbumArt(album_id);
//		Bitmap bm = null;
//		if (albumArt == null) {
//			// mImageView.setBackgroundResource(R.drawable.staring);
//		} else {
//			bm = BitmapFactory.decodeFile(albumArt);
//			bmpDraw = new BitmapDrawable(bm);
//			// mImageView.setImageDrawable(bmpDraw);
//		}
//		return bmpDraw;
//	}
	
	/**
	 * 获取album_id
	 * @param musicPath
	 * @return
	 */
	public String  getAlbum(String musicPath) {
		Cursor currentCursor = getCursor(musicPath);
		String album = currentCursor.getString(currentCursor
				.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
		
		return album;
	}
	
	/**
	 * 获取歌曲名字
	 * @param musicPath
	 * @return
	 */
	public String  getMusicName(String musicPath) {
		Cursor currentCursor = getCursor(musicPath);
		String tilte =currentCursor.getString(currentCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
		return tilte;
	}
	/**
	 * 获取歌手名字
	 * @param musicPath
	 * @return
	 */
	public String  getSingerName(String musicPath) {
		Cursor currentCursor = getCursor(musicPath);
		// 歌曲的歌手名： MediaStore.Audio.Media.ARTIST  
		String artist =currentCursor.getString(currentCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));  
		return artist;
	}
	
	
	
}
